# -*- coding: utf8 -*-

import os
from sqlalchemy import Column, Integer, String, Text, BigInteger, SmallInteger, Boolean
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()


class Option(Base):
    __tablename__ = 'option'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    name = Column(String(255), index=True)
    value = Column(Text)

    def __init__(self, name, value):
        self.name = name
        self.value = value

    def __repr__(self):
        return "<Option(id=%s)(name='%s', value='%s')>" % (
            self.id, self.name, self.value)

    pass


class Directory(Base):
    __tablename__ = 'directory'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, index=True)
    device = Column(SmallInteger)
    name = Column(String(255))
    
    def __init__(self, parent_id, device, name):
        self.parent_id = parent_id
        self.device = device
        self.name = name

    def __repr__(self):
        return "<Directory(id=%s)(parent_id='%s', device='%s', name='%s')>" % (
            self.id, self.parent_id, self.device, self.name)

    def __str__(self):
        return "<Directory(id=%s)(parent_id='%s', device='%s', name='%s')>" % (
            self.id, self.parent_id, self.device, self.name)

    def __unicode__(self):
        return "<Directory(id=%s)(parent_id='%s', device='%s', name='%s')>" % (
            self.id, self.parent_id, self.device, self.name)

    pass


class HashFile(Base):
    __tablename__ = 'hash_file'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    hash_id = Column(Integer, primary_key = True)
    file_id = Column(Integer, primary_key = True)

    def __init__(self, hash_id, file_id):
        self.hash_id = hash_id
        self.file_id = file_id

    def __repr__(self):
        return "<HashFile(hash_id='%s', file_id='%s')>" % (
            self.hash_id, self.file_id)

    pass


class Hash(Base):
    __tablename__ = 'hash'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    value = Column(String(255), index=True)

    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return "<Hash(id=%s)(value='%s')>" % (
            self.id, self.value)

    pass


class File(Base):
    __tablename__ = 'file'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    dir_id = Column(Integer, index=True)
    file_id = Column(Integer, index=True)
    device = Column(SmallInteger)
    mime = Column(String(255))
    size = Column(BigInteger)
    mtime = Column(Integer)
    name = Column(String(255))

    _full_path = None

    def __init__(self, dir_id, device, mime, size, mtime, name, file_id = None):
        self.dir_id = dir_id
        self.file_id = file_id
        self.device = device
        self.mime = mime
        self.size = size
        self.mtime = mtime
        self.name = name

    def __repr__(self):
        return "<File(id=%s)(dir_id='%r', file_id='%r', device='%s', mime='%s', size='%s', mtime='%s', name='%s')>" % (
            self.id, self.dir_id, self.file_id, self.device, self.mime, self.size, self.mtime, self.name)


    def get_full_path( self, sess ):

        if self._full_path:
            return self._full_path

        paths = [ self.name ]

        parent = self.dir_id
        while parent:
            dir_item = sess.query(Directory).filter_by(id=parent).first()
            paths.append( dir_item.name )
            parent = dir_item.parent_id
            sess.expunge(dir_item)

        # print("File: paths=%r" % (paths,))
        paths.reverse()
        # print("File: reverted paths=%r" % (paths,))

        self._full_path = os.sep.join(paths)

        return self._full_path

    pass

class Image(Base):
    __tablename__ = 'image'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    file_id = Column(Integer, index=True)
    name = Column(Text)
    animation = Column(Boolean)
    mime = Column(String(255))
    size = Column(Integer)
    is_gray = Column(Boolean, index=True)

    width = Column(SmallInteger)
    height = Column(SmallInteger)
    mode = Column(String(4))

    mean_gray = Column(SmallInteger, index=True)


    def __init__(self, file_id, name, animation, mime, size, is_gray,
                 width, height, mode, mean_gray):
        self.file_id = file_id
        self.name = name
        self.mime = mime
        self.size = size
        self.animation = animation
        self.is_gray = is_gray
        self.width = width
        self.height = height
        self.mode = mode
        self.mean_gray = mean_gray

    def __repr__(self):
        return ("<Image(id=%s)(file_id='%s', name='%s', animation=%r, mime='%s', size='%s',"+\
               " is_gray=%r, width='%s', height='%s', mode='%s', mean_gray='%s')>") % (
            self.id, self.file_id, self.name, self.animation, self.mime, self.size,
            self.is_gray, self.width, self.height, self.mode, self.mean_gray)

    pass


class ImageVector10(Base):
    __tablename__ = 'image_vector10'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    image_id = Column(Integer, index=True)
    orientation = Column(SmallInteger)

    v0 = Column(SmallInteger, index=True)
    v1 = Column(SmallInteger, index=True)
    v2 = Column(SmallInteger, index=True)
    v3 = Column(SmallInteger, index=True)
    v4 = Column(SmallInteger, index=True)
    v5 = Column(SmallInteger, index=True)
    v6 = Column(SmallInteger, index=True)
    v7 = Column(SmallInteger, index=True)
    v8 = Column(SmallInteger, index=True)
    v9 = Column(SmallInteger, index=True)


    def __init__(self, image_id, orientation, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9):
        self.image_id = image_id
        self.orientation = orientation
        self.v0 = v0
        self.v1 = v1
        self.v2 = v2
        self.v3 = v3
        self.v4 = v4
        self.v5 = v5
        self.v6 = v6
        self.v7 = v7
        self.v8 = v8
        self.v9 = v9
        pass

    def __repr__(self):
        return "<ImageVector10(id=%s)(image_id='%s', orientation='%s', vector='%r')>" % (
            self.id, self.image_id, self.orientation, [
                self.v0, self.v1, self.v2, self.v3, self.v4, self.v5, self.v6, self.v7, self.v8, self.v9
            ])

    pass

class ImageVector100(Base):
    __tablename__ = 'image_vector100'
    # __table_args__ = {'mysql_engine':'InnoDB'}

    id = Column(Integer, primary_key=True)
    image_id = Column(Integer, index=True)
    orientation = Column(SmallInteger)
    vector = Column(Text)


    def __init__(self, image_id, orientation, vector):
        self.image_id = image_id
        self.orientation = orientation
        self.vector = vector

    def __repr__(self):
        return "<ImageVector100(id=%s)(image_id='%s', orientation='%s', vector='%s')>" % (
            self.id, self.image_id, self.orientation, self.vector)

    pass
